/// @author  Alberto Franco
/// @file    barber.go
/// @date    02-05-2011
/// @brief   This in the implementation of the barber shop problem as found in
///          "The Little Book of Semaphores" by Allen B. Downey 
///          Barber data structure
package barbershop

import (
    "time"
    "rand"
)

/// Barber can sleep or cut hairs when he there are customers in the shop
/// Barber = sleep.Barber + cutHair.Barber
type Barber struct {
    Chair chan *Customer ///< The chair used as 
}

/// Serve all customers 
func (self *Barber) Routine() {
    var customer *Customer
    for {
        /// Hold on for a customer
        customer = <- self.Chair
        /// Cut hair and wait a random time
        time.Sleep(rand.Int63n(1000))
        /// Notify the customer
        customer.CutHair <- 1
    }
}

/// Create a customer
func CreateBarber() *Barber {
    barber := new(Barber)
    barber.Chair = make (chan *Customer)
    return barber
}
